import { api } from './api';
import type { User, UserFormData, UserResponse } from '../types/user';

// 获取所有用户
export const getAllUsers = async (): Promise<User[]> => {
  const response = await api.get<UserResponse>('/users');
  if (Array.isArray(response.data.data)) {
    return response.data.data;
  }
  return [];
};

// 获取单个用户
export const getUserById = async (id: string): Promise<User | null> => {
  try {
    const response = await api.get<UserResponse>(`/users/${id}`);
    if (!Array.isArray(response.data.data)) {
      return response.data.data as User;
    }
    return null;
  } catch (error) {
    console.error('获取用户失败:', error);
    return null;
  }
};

// 创建用户
export const createUser = async (userData: UserFormData): Promise<User | null> => {
  try {
    const response = await api.post<UserResponse>('/users', userData);
    if (!Array.isArray(response.data.data)) {
      return response.data.data as User;
    }
    return null;
  } catch (error) {
    console.error('创建用户失败:', error);
    return null;
  }
}; 